package com.nondo.dean.community.service;

import com.nondo.dean.auth.common.TokenPrincipalProvider;
import com.nondo.dean.common.tool.AliyunOssTool;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.DateProcessUtils;
import com.nondo.dean.community.domain.Article;
import com.nondo.dean.community.domain.UserArticle;
import com.nondo.dean.community.domain.UserArticleExample;
import com.nondo.dean.community.mapper.UserArticleMapper;
import com.nondo.dean.community.model.ArticleBean;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaofan
 * @Description: 文章收藏业务管理类
 * @throws
 * @date 2018-05-0413:55
 * @Version
 */
@Component
public class UserArticleService {

    @Autowired
    private UserArticleMapper userArticleMapper;

    @Autowired
    TokenPrincipalProvider tokenPrincipalProvider;

    @Autowired
    ArticleService articleService;

    @Autowired
    AliyunOssTool aliyunOssTool;

    /**
     * 添加收藏
     *
     * @param userArticle
     * @return
     */
    public int add(UserArticle userArticle) {
        Integer userId = tokenPrincipalProvider.getTokenPrincipal().getUserId().intValue();
        userArticle.setUserId(userId);
        userArticle.setStatus(1);
        return this.userArticleMapper.insertSelective(userArticle);
    }

    /**
     * 取消收藏
     * @param userArticle
     * @return
     */
    public int update(UserArticle userArticle) {
        Integer userId = tokenPrincipalProvider.getTokenPrincipal().getUserId().intValue();
        userArticle.setStatus(0);
        UserArticleExample example =new UserArticleExample();
        example.createCriteria().andUserIdEqualTo(userId).andArticleIdEqualTo(userArticle.getArticleId());
        return this.userArticleMapper.updateByExampleSelective(userArticle,example);
    }

    /**
     * 查询用户收藏的文章
     * @param offset
     * @param limit
     * @return newList
     */
    public List<ArticleBean> queryUserCollectArticles(int offset, int limit){
         List<ArticleBean>  newList =new ArrayList<>();
        RowBounds rowBounds = new RowBounds(offset, limit);
        //TODO 获取当前登录用户id
         Integer userId =1;// tokenPrincipalProvider.getTokenPrincipal().getUserId().intValue();
         UserArticleExample userArticleExample = new UserArticleExample();
         userArticleExample.createCriteria().andStatusEqualTo(1).andUserIdEqualTo(userId);
         List<UserArticle> userArticleList = userArticleMapper.selectByExampleWithRowbounds(userArticleExample,rowBounds);
         for(UserArticle userArticle : userArticleList){
             Article article = articleService.select(userArticle.getArticleId());
             ArticleBean articleBean = new ArticleBean();
             BeanUtils.copy(article, articleBean);
             //日期处理
             articleBean.setTimeDate(DateProcessUtils.showTimeText(article.getCreateDate()));
             //图片
             articleBean.setImage(this.aliyunOssTool.getImgUrl(articleBean.getImage()));
             newList.add(articleBean);
         }
         return newList;
    }

    /**
     * 数据统计
     *
     * @param article
     * @return
     */
    public Long count(UserArticle article) {
        UserArticleExample example = new UserArticleExample();
        UserArticleExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, article);
        return userArticleMapper.countByExample(example);
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param userArticle
     */
    void setCriteria(UserArticleExample.Criteria criteria, UserArticle userArticle) {
        if (userArticle != null) {
            if (userArticle.getId() != null) {
                criteria.andIdEqualTo(userArticle.getId());
            }
            if (userArticle.getArticleId() !=null) {
                criteria.andArticleIdEqualTo(userArticle.getArticleId());
            }

            if (userArticle.getStatus() != null) {
                criteria.andStatusEqualTo(userArticle.getStatus());
            }
            if (userArticle.getUserId() != null) {
                criteria.andStatusEqualTo(userArticle.getUserId());
            }

        }
        criteria.andDelFlagEqualTo(UserArticle.DEL_FLAG_NORMAL);
    }
}
